package com.company.project.controller;

import com.company.project.core.BaseController;
import com.company.project.core.MsgMap;
import com.company.project.utils.LoggerUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * IntelliJ IDEA.
 * User: lwb
 * Date: 2018/1/19
 * Time: 16:24
 * function:
 */

@Controller
@RequestMapping("/login")
public class LoginController extends BaseController
{

    @RequestMapping("checkLogin")
    @ResponseBody
    public MsgMap checkLogin(HttpServletRequest request)
    {
        MsgMap msg = this.getMsgMap();
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        try
        {
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isAuthenticated())
            {
                subject.login(token);//用户认证
            }

        }catch (Exception e)
        {
            LoggerUtils.error(e.getMessage() , e);
            msg.putMsg("用户名或密码错误");
            msg.putCodeError();
        }
        return  msg;
    }

}
